package hk.ust.comp3111.ustnutrition;

import hk.ust.comp3111.ustnutrition.data.ApiHandler;
import hk.ust.comp3111.ustnutrition.data.DataAccessor;
import hk.ust.comp3111.ustnutrition.data.json.ApiResponseBase;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnShowListener;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.ContextThemeWrapper;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

public class LoginDialog extends DialogFragment
{
	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setStyle(DialogFragment.STYLE_NO_FRAME,
				android.R.style.Theme_Holo_Light_Dialog_NoActionBar);
	}

	@Override
	public Dialog onCreateDialog(Bundle savedInstanceState)
	{
		ContextThemeWrapper theme = new ContextThemeWrapper(getActivity(),
				R.style.Theme_AppCompat_Light);
		LayoutInflater inflater = LayoutInflater.from(getActivity())
				.cloneInContext(theme);
		final View dialogView = inflater.inflate(R.layout.login_dialog, null);

		mEmail = (TextView)dialogView.findViewById(R.id.email);
		mEmail.setText(DataAccessor.Instance.getUser().getEmail());

		mCode = (TextView)dialogView.findViewById(R.id.code);
		mCode.addTextChangedListener(new TextWatcher()
				{
					@Override
					public void onTextChanged(CharSequence s, int start,
							int before, int count)
					{
						mButton.setText((mCode.length() == 0) ? "Login"
								: "Validate");
					}

					@Override
					public void beforeTextChanged(CharSequence s, int start,
							int count, int after)
					{}

					@Override
					public void afterTextChanged(Editable s)
					{}
				});

		AlertDialog.Builder builder = new AlertDialog.Builder(theme);
		final AlertDialog d = builder.setTitle("Login")
				.setView(dialogView)
				.setPositiveButton("Login", null)
				.setNegativeButton(android.R.string.cancel, null)
				.create();

		final OnClickListener btnListener = new OnClickListener()
				{
					@Override
					public void onClick(View v)
					{
						if (!TextUtils.isEmpty(mEmail.getText()))
						{
							if (!TextUtils.isEmpty(mCode.getText()))
							{
								onValidateClick();
							}
							else
							{
								OnLoginClick();
							}
						}
					}
				};
		d.setOnShowListener(new OnShowListener()
				{
					@Override
					public void onShow(DialogInterface dialog)
					{
						mButton = d.getButton(DialogInterface.BUTTON_POSITIVE);
						mButton.setOnClickListener(btnListener);
					}
				});
		return d;
	}

	private void OnLoginClick()
	{
		AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
		ProgressBar bar = new ProgressBar(getActivity(), null,
				android.R.attr.progressBarStyleHorizontal);
		bar.setIndeterminate(true);
		builder.setCancelable(false)
				.setTitle("Sending request to server")
				.setView(bar);
		final AlertDialog dialog = builder.show();
		DataAccessor.Instance.login(mEmail.getText().toString(),
				new ApiHandler<ApiResponseBase>()
				{
					@Override
					public void onSuccess(ApiResponseBase response)
					{
						dialog.dismiss();
						Toast.makeText(getActivity(), response.getMessage(),
								Toast.LENGTH_LONG).show();
					}

					@Override
					public void onFailure(String message)
					{
						dialog.dismiss();
						if (message == null)
						{
							Toast.makeText(getActivity(), "Unknown error",
									Toast.LENGTH_LONG).show();
						}
						else
						{
							Toast.makeText(getActivity(), message,
									Toast.LENGTH_LONG).show();
						}
					}
				});
	}

	private void onValidateClick()
	{
		AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
		ProgressBar bar = new ProgressBar(getActivity(), null,
				android.R.attr.progressBarStyleHorizontal);
		bar.setIndeterminate(true);
		builder.setCancelable(false)
				.setTitle("Sending request to server")
				.setView(bar);
		final AlertDialog dialog = builder.show();
		DataAccessor.Instance.validate(mEmail.getText().toString(),
				mCode.getText().toString(),
				new ApiHandler<ApiResponseBase>()
				{
					@Override
					public void onSuccess(ApiResponseBase response)
					{
						dialog.dismiss();
						Toast.makeText(getActivity(), response.getMessage(),
								Toast.LENGTH_LONG).show();
						dismiss();
					}

					@Override
					public void onFailure(String message)
					{
						dialog.dismiss();
						if (message == null)
						{
							Toast.makeText(getActivity(), "Unknown error",
									Toast.LENGTH_LONG).show();
						}
						else
						{
							Toast.makeText(getActivity(), message,
									Toast.LENGTH_LONG).show();
						}
					}
				});
	}

	private TextView mEmail;
	private TextView mCode;
	private Button mButton;
}
